import getUUID from '../utils/getUUID.js'
import { getPagePath } from '../utils/common.js'
import { lazyReportCache } from '../utils/report.js'

export default function pageChange() {
    let from = ''
    let oldUrl = ''
    window.addEventListener('popstate', () => {
        const to = getPagePath()
        const data = {
            type: 'behavior',
            subType: 'popstate',
            startTime: performance.now(),
            from,
            to,
            uuid: getUUID(),
        }
        lazyReportCache(data)
        from = to
    }, true)
    window.addEventListener('hashchange', event => {
        const newUrl = event.newURL
        const data = {
            from: oldUrl,
            to: newUrl,
            type: 'behavior',
            subType: 'hashchange',
            startTime: performance.now(),
            uuid: getUUID(),
        }
        lazyReportCache(data)
        oldUrl = newUrl
    }, true)
}
